package com.jinxin.platform.cdqzsqacs.task;

import com.alibaba.fastjson.JSON;
import com.jinxin.platform.cdqzsqacs.bean.CdqzsqacsDevice;
import com.jinxin.platform.cdqzsqacs.bean.CdqzsqacsOpenRecord;
import com.jinxin.platform.cdqzsqacs.bean.gaEntity.Ga_cjsbztxx;
import com.jinxin.platform.cdqzsqacs.bean.gaEntity.Ga_cjxtxx;
import com.jinxin.platform.cdqzsqacs.bean.gaEntity.Ga_cjxtztxx;
import com.jinxin.platform.cdqzsqacs.bean.gaEntity.Ga_mjjcjlxx;
import com.jinxin.platform.cdqzsqacs.common.Constant;
import com.jinxin.platform.cdqzsqacs.redis.RedisUtil;
import com.jinxin.platform.cdqzsqacs.service.GaUploadService;
import com.jinxin.platform.cdqzsqacs.service.IQzsqAcsDeviceService;
import com.jinxin.platform.cdqzsqacs.service.IQzsqAcsRecordService;
import com.jinxin.platform.cdqzsqacs.service.impl.GaUploadAsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-06-01
 * @Author: Lingnan
 **/
@EnableScheduling
@Configuration
@Slf4j
@EnableAsync
public class CdqzsqacsTask {
    @Autowired
    private IQzsqAcsDeviceService deviceService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IQzsqAcsRecordService recordService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private GaUploadService gaUploadService;
    @Autowired
    private GaUploadAsyncService gaUploadAsyncService;

//    private ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
//            new ArrayBlockingQueue<>(5));

    @Scheduled(cron = "0 0 */1 * * ?  ")
    public void incoming(){
        String operateAdd = "add";
        String operateUpdate = "update";
        String serverPort = serverConfig.getServerPort() + "";
        List<CdqzsqacsDevice> deviceList = deviceService.list(new HashMap());
        List<Ga_cjsbztxx> cjsbztxxList = new ArrayList<>();
        // 上传设备状态到公安平台
        deviceList.forEach(deviceInfo->{
            String sjsj = (int) (System.currentTimeMillis() / 1000) + "";
            String operateType1 = deviceInfo.getOperateType();
            String operateType = StringUtils.isEmpty(operateType1) ? operateAdd : operateUpdate;
            Ga_cjsbztxx cjsbxx = Ga_cjsbztxx.builder().CJSBXXBZ(deviceInfo.getId()).CJSJ(sjsj).
                    SJLY(Constant.DEVICE_SYLY).OPERATE_TYPE(operateType).build();
            cjsbztxxList.add(cjsbxx);
        });
        String jsonString = JSON.toJSONString(cjsbztxxList);
        gaUploadAsyncService.gaUploadData("ga_cjsbztxx", jsonString,false,"");
        deviceList.forEach(deviceInfo->{
            deviceInfo.setOperateType(operateAdd);
            deviceService.saveOrUpdate(deviceInfo);
        });
        // 上传采集系统对象到公安平台
        String cjxtType = "ga_cjxtxx";
        String key = Constant.MODULE_KEY + "-operate-type-" + cjxtType;
        String operation = (String) redisUtil.get(key);
        if(StringUtils.isEmpty(operation)){
            Ga_cjxtxx cjxtxx = Ga_cjxtxx.builder().CJXTXXBZ("1").XTMC("海洋社区智慧社区平台").WLDKH(serverPort)
                    .SFZX_PDBZ("1").XQXXBZ("").SJLY(Constant.DEVICE_SYLY).OPERATE_TYPE(operateAdd).build();
            gaUploadAsyncService.gaUploadData("ga_cjxtxx", jsonString,false,"");
            gaUploadAsyncService.gaUploadData("ga_cjxtxx", jsonString,false,"");
            redisUtil.set(key,"update");
            log.info("上传成功采集系统");
        }else {
            Ga_cjxtxx cjxtxx = Ga_cjxtxx.builder().CJXTXXBZ("1").XTMC("海洋社区智慧社区平台").WLDKH(serverPort)
                    .SFZX_PDBZ("1").XQXXBZ("").SJLY(Constant.DEVICE_SYLY).OPERATE_TYPE(operation).build();
            gaUploadAsyncService.gaUploadData("cjxtxx", JSON.toJSONString(cjxtxx),false,"");
            //gaUploadService.incoming("ga_cjxtxx", JSON.toJSONString(cjxtxx));
            //executor.submit(new IncomingTask(gaUploadService,"ga_cjxtxx", JSON.toJSONString(cjxtxx)));
        }
        // 采集系统状态对象数据结构
        String cjxtztType = "ga_cjxtzt";
        String key2 = Constant.MODULE_KEY + "-operate-type-" + cjxtztType;
        String operation2 = (String) redisUtil.get(key2);
        String sjsj = (int) (System.currentTimeMillis() / 1000) + "";
        if(StringUtils.isEmpty(operation2)){
            Ga_cjxtztxx cjxtxx = Ga_cjxtztxx.builder().CJXTXXBZ("1").CJSJ(sjsj)
                    .SFZX_PDBZ("1").SJLY(Constant.DEVICE_SYLY).OPERATE_TYPE(operateAdd).build();
            gaUploadAsyncService.gaUploadData("ga_cjxtztxx", JSON.toJSONString(cjxtxx),false,"");
//            gaUploadService.incoming("ga_cjxtztxx", JSON.toJSONString(cjxtxx));
            try {
                ResultEntity resultEntity = gaUploadService.incoming("ga_cjxtztxx", JSON.toJSONString(cjxtxx),false,"");
                if(resultEntity.getCode() == Constant.INCOMING_RESULT_SUCCESS){
                    redisUtil.set(key2,"update");
                    log.info("上传成功采集系统状态");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            Ga_cjxtztxx cjxtxx = Ga_cjxtztxx.builder().CJXTXXBZ("1").CJSJ(sjsj)
                    .SFZX_PDBZ("1").SJLY(Constant.DEVICE_SYLY).OPERATE_TYPE(operateUpdate).build();
            gaUploadAsyncService.gaUploadData("ga_cjxtztxx", JSON.toJSONString(cjxtxx),false,"");
            //gaUploadService.incoming("ga_cjxtztxx", JSON.toJSONString(cjxtxx));
        }
        // 门禁进出数据第一次新增
        String mjjcxxType = "ga_mjjcxx";
        String key3 = Constant.MODULE_KEY + "-operate-type-" + mjjcxxType;
        String operation3 = (String) redisUtil.get(key3);
        if(StringUtils.isEmpty(operation3)){
            List<CdqzsqacsOpenRecord> recordList = recordService.list(new HashMap());
            List<Ga_mjjcjlxx> mjjcxxList = new ArrayList<>();
            recordList.forEach(openRecord->{
                String cjsj = (int) (System.currentTimeMillis() / 1000) + "";
                Ga_mjjcjlxx cjsbxx = Ga_mjjcjlxx.builder().MJJCJLXXBZ(openRecord.getId()).XM(openRecord.getOpenUser())
                        .GMSFZHM(openRecord.getCardNo()).PZH("").MJDBH("").SK_RQSJ(openRecord.getOpenTime().toString())
                        .MCZT("1").GLDMJ_CJSBXXBZ(openRecord.getSerialNum()).SFTG_PDBZ("1").KMFSDM("4").KMJG("1")
                        .MJSJLX("0").CJSJ(cjsj).XQXXBZ("noTimeFormatRequired@"+openRecord.getCommunityId()).SJLY(Constant.DEVICE_SYLY).OPERATE_TYPE(operateAdd).build();
                mjjcxxList.add(cjsbxx);
            });
            if(mjjcxxList.size() > 0){
                gaUploadAsyncService.gaUploadData("ga_mjjcjlxx", JSON.toJSONString(mjjcxxList),false,"");
                redisUtil.set(key3,"update");
                log.info("上传成功{}条门禁进出数",mjjcxxList.size());
            }
        }

    }

    // 每天2点执行一次
//    @Scheduled(cron = "0 0 2 * * ?  ")
//    public void incomingMjjcxx(){
//        HashMap param = new HashMap();
//        String yesterDay = DateUtil.getLastDay();
//        param.put("startDate",yesterDay+" 00:00:00");
//        param.put("endDate",yesterDay+" 23:59:59");
//        List<CdqzsqacsOpenRecord> recordList = recordService.list(param);
//        List<Ga_mjjcxx> mjjcxxList = new ArrayList<>();
//        recordList.forEach(openRecord->{
//            String cjsj = (int) (System.currentTimeMillis() / 1000) + "";
//            Ga_mjjcxx cjsbxx = Ga_mjjcxx.builder().MJJCJLXXBZ(openRecord.getId()).XM(openRecord.getOpenUser())
//                    .GMSFZHM(openRecord.getCardNo()).PZH("").MJDBH("").SK_RQSJ(openRecord.getOpenTime().toString())
//                    .MCZT("1").GLDMJ_CJSBXXBZ(openRecord.getSerialNum()).SFTG_PDBZ("1").KMFSDM("4").KMJG("1")
//                    .MJSJLX("0").CJSJ(cjsj).XQXXBZ(openRecord.getCommunityId()).SJLY(Constant.DEVICE_SYLY).OPERATE_TYPE("add").build();
//            mjjcxxList.add(cjsbxx);
//        });
//        if(mjjcxxList.size() > 0){
//            Future<ResultEntity> future = executor.submit(new IncomingTask(gaUploadService,"ga_mjjcxx", JSON.toJSONString(mjjcxxList)));
//            try {
//                ResultEntity resultEntity = future.get();
//                if(resultEntity.getCode() == Constant.INCOMING_RESULT_SUCCESS){
//                    log.info("上传成功{}条门禁进出数",mjjcxxList.size());
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
//    }

}
